<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class order extends check{
	//构造函数
	public function __construct(){
		parent::__construct();
		$this->odstarow = array(0=>'未付款', 1=>'已付款', 2=>'已发货', 3=>'已失效');
	}
	//订单列表
	public function lis(){
		$where = ' 1 = 1 ';
		if($search = $this->spArgs('search')){
			if($search['status'] != '-1'){
				$where .= " AND status = {$search[status]}";
			}
			if($usercode = trim($search['usercode'])){
				if($uinfo = spClass('m_user')->find(array('usercode'=>$usercode))){
					$uid = $uinfo['id'];
				}else{
					$uid = 0;
				}
				$where .= " AND uid = {$uid}";
				$search['usercode'] = $usercode;
			}
			if($stime = strtotime($search['stime'])){
				$where .= " AND time>=" . $stime;
				$search['stime'] = $stime;
			}
			if($etime = strtotime($search['etime'])){
				$where .= " AND time<=" . $etime;
				$search['etime'] = $etime;
			}
			$this->search = $search;
		}else{
			if($status = $this->spArgs('status') && $status != '-1'){
				$where .= " AND status = {$status}";
				$search['status'] = $status;
			}
			if($usercode = $this->spArgs('usercode')){
				$usercode = urldecode($usercode);
				if($uinfo = spClass('m_user')->find(array('usercode'=>$usercode))){
					$uid = $uinfo['id'];
				}else{
					$uid = 0;
				}
				$where .= " AND uid = {$uid}";
				$search['usercode'] = $usercode;
			}
			if($stime = $this->spArgs('stime')){
				$where .= " AND time>=" . $stime;
				$search['stime'] = $stime;
			}
			if($etime = $this->spArgs('etime')){
				$where .= " AND time<=" . $etime;
				$search['etime'] = $etime;
			}
			$this->search = $search;
		}
		$page = (int) $this->spArgs('page',1) > 1 ? (int) $this->spArgs('page',1) : 1;
		$order = spClass('m_order')->spPager($page,15)->findAll($where,'id DESC');
		foreach($order as &$v){
			$v['uinfo'] = spClass('m_user')->find(array('id'=>$v['uid']));
		}
		$this->order = $order;
		$this->pager = spClass('m_order')->spPager()->getPager();
		$this->display('order_lis.tpl.php');
	}
	//订单明细
	public function show(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('order','lis'));
		if(!$order = spClass('m_order')->find(array('id'=>$id))) $this->error('订单信息不存在！', spUrl('order', 'lis'));
		if(!$user = spClass('m_user')->find(array('id'=>$order['uid']))) $this->error('会员信息不存在！', spUrl('order', 'lis'));
		$order['info'] = unserialize($order['info']);
		$order['needupd'] = false;
		$order['totalmoney'] = 0;
		foreach($order['info'] as $k=>&$v){
			if(!$prod = spClass('m_prod')->find(array('id'=>(int)$k),null,'pname,price,danwei,nosale')){
				$v['status'] = 'delete'; //产品被删除
				$order['needupd'] = true;
			}elseif($prod['nosale'] == 1){
				$v['status'] = 'nosale'; //产品已下架
				$order['needupd'] = true;
			}else{
				$v['status'] = 'normal'; //正常状态
			}
			//只有未确认状态才获取产品最新信息
			if(!empty($prod) && $order['status']==0){
				$prod['price'] = unserialize($prod['price']);
				$v['price'] = $prod['price'][$user['groupid']];
				$v['pname'] = $prod['pname'];
				$v['danwei'] = $prod['danwei'];
			}
			if($v['status'] == 'normal'){
				$order['totalmoney'] += $v['price'] * $v['number'];
			}
		}
		//只有未确认状态才显示[新总金额]
		if($order['status'] != 0){
			$order['needupd'] = false;
		}
		$order['user'] = spClass('m_user')->spLinker()->find(array('id'=>$order['uid']));
		if($order['status'] == 0){
			$this->odstarow = array(0=>'未付款', 1=>'已付款', 3=>'已失效');
		}elseif($order['status'] == 1){
			$this->odstarow = array(1=>'已付款', 2=>'已发货', 3=>'已失效');
		}elseif($order['status'] == 2){
			$this->odstarow = array(2=>'已发货', 3=>'已失效');
		}elseif($order['status'] == 3){
			$this->odstarow = array(3=>'已失效');
		}
		$this->order = $order;
		$this->display('order_show.tpl.php');
	}
	//订单打印
	public function odprint(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('order','lis'));
		if(!$order = spClass('m_order')->find(array('id'=>$id))) $this->error('订单信息不存在！', spUrl('order', 'lis'));
		if(!$user = spClass('m_user')->find(array('id'=>$order['uid']))) $this->error('会员信息不存在！', spUrl('order', 'lis'));
		$order['info'] = unserialize($order['info']);
		foreach($order['info'] as $k=>&$v){
			$prod = spClass('m_prod')->find(array('id'=>(int)$k),null,'bianhao');
			$v['bianhao'] = $prod['bianhao'];
		}
		$this->user = $user;
		$this->order = $order;
		$this->display('order_print.tpl.php');
	}
	//更新状态
	public function updst(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('order','lis'));
		if(!$order = spClass('m_order')->find(array('id'=>$id))) $this->error('订单号不存在！', spUrl('order','lis'));
		if(!$note = trim($this->spArgs('note'))) $this->error('必须填写备注信息！', spUrl('order','lis'));
		$rows = $this->spArgs();
		$osta = (int) $rows['osta'];
		$nsta = (int) $rows['status'];
		$kuaidi = trim($rows['kuaidi']);
		$danhao = trim($rows['danhao']);
		$totalmoney = sprintf('%.2f', $rows['totalmoney']);
		if(false != spClass('m_order')->update(array('id'=>$id),array('status'=>$nsta,'kuaidi'=>$kuaidi,'danhao'=>$danhao))){
			//记录订单状态修改日志
			spClass('m_odlog')->create(array('oid'=>$id,'aid'=>$_SESSION['vonuo']['id'],'osta'=>$osta,'nsta'=>$nsta,'note'=>$note,'time'=>time()));
			//未付款->已付款
			if($osta == 0 && $nsta == 1){
				//记录成功订单日志
				for($i=0;$i<count($rows['pid']);$i++){
					spClass('m_prod')->query("UPDATE vn_prod SET salenum = salenum + {$rows['number'][$i]} WHERE id = {$rows['pid'][$i]}");
					spClass('m_odsuc')->create(array('oid'=>$id,'uid'=>$order['uid'],'time'=>$order['time'],'pid'=>$rows['pid'][$i],'price'=>$rows['price'][$i],'number'=>$rows['number'][$i],'money'=>sprintf('%.2f',$rows['price'][$i]*$rows['number'][$i])));
				}
				//更新订单实际信息
				$orderinfo = array();
				for($i=0;$i<count($rows['pid']);$i++){
					$orderinfo[$rows['pid'][$i]] = array('number'=>$rows['number'][$i],'price'=>$rows['price'][$i],'pname'=>$rows['pname'][$i],'danwei'=>$rows['danwei'][$i]);
				}
				$orderinfo = serialize($orderinfo);
				if(get_magic_quotes_gpc()){
					$orderinfo = addslashes($orderinfo);
				}
				spClass('m_order')->update(array('id'=>$id),array('info'=>$orderinfo,'money'=>$totalmoney));
				//增加会员积分
				$jifen = round($totalmoney);
				spClass('m_user')->query("UPDATE vn_user SET jifen = jifen + {$jifen} WHERE id = {$order['uid']}");
			}
			//未付款-已发货
			if($osta == 0 && $nsta == 2){
				//TODO 该情况不存在
			}
			//未付款-已失效
			if($osta == 0 && $nsta == 3){
				//TODO 无需操作
			}
			//已付款-已发货
			if($osta == 1 && $nsta == 2){
				//TODO 无需操作
			}
			//已付款-已失效
			if($osta == 1 && $nsta == 3){
				//删除成功订单日志
				spClass('m_odsuc')->delete(array('oid'=>$id));
				//TODO 减少会员积分
			}
			//已发货->已失效
			if($osta == 2 && $nsta == 3){
				//删除成功订单日志
				spClass('m_odsuc')->delete(array('oid'=>$id));
				//TODO 减少会员积分
			}
			//状态提示
			$this->success('更新订单状态成功！', spUrl('order','lis'));
		}else{
			$this->error('更新订单状态失败！', spUrl('order','lis'));
		}
	}
	//更新物流
	public function updwl(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('order','lis'));
		if(!$order = spClass('m_order')->find(array('id'=>$id))) $this->error('订单号不存在！', spUrl('order','lis'));
		$rows = array('kuaidi'=>trim($this->spArgs('kuaidi')), 'danhao'=>trim($this->spArgs('danhao')));
		if(false != spClass('m_order')->update(array('id'=>$id),$rows)){
			$this->success('更新物流信息成功！', spUrl('order','show',array('id'=>$id)));
		}else{
			$this->error('更新物流信息失败！', spUrl('order','show',array('id'=>$id)));
		}
	}
	//更新订单备注信息
	public function updost(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('order','lis'));
		if(false != spClass('m_order')->update(array('id'=>$id),array('note'=>$this->spArgs('note_order')))){
			$this->success('更新备注信息成功！', spUrl('order','show',array('id'=>$id)));
		}else{
			$this->error('更新备注信息失败！', spUrl('order','show',array('id'=>$id)));
		}
	}
	//日志列表
	public function log(){
		$this->admin = spClass('m_admin')->findAll(null,'id ASC','id,name');
		$where = ' 1 = 1 ';
		if($search = $this->spArgs('search')){
			if($aid = (int) $search['aid']){
				$where .= " AND aid = {$aid}";
			}
			if($oid = (int) $search['oid']){
				$where .= " AND oid = {$oid}";
			}
			if($stime = strtotime($search['stime'])){
				$where .= " AND time>=" . $stime;
				$search['stime'] = $stime;
			}
			if($etime = strtotime($search['etime'])){
				$where .= " AND time<=" . $etime;
				$search['etime'] = $etime;
			}
			$this->search = $search;
		}else{
			if($aid = (int)$this->spArgs('aid')){
				$where .= " AND aid = {$aid}";
				$search['aid'] = $aid;
			}
			if($oid = (int)$this->spArgs('oid')){
				$where .= " AND oid = {$oid}";
				$search['oid'] = $oid;
			}
			if($stime = $this->spArgs('stime')){
				$where .= " AND time>=" . $stime;
				$search['stime'] = $stime;
			}
			if($etime = $this->spArgs('etime')){
				$where .= " AND time<=" . $etime;
				$search['etime'] = $etime;
			}
			$this->search = $search;
		}
		$page = (int) $this->spArgs('page',1) > 1 ? (int) $this->spArgs('page',1) : 1;
		$this->odlog = spClass('m_odlog')->spLinker()->spPager($page,15)->findAll($where,'id DESC');
		$this->pager = spClass('m_order')->spPager()->getPager();
		$this->display('order_log.tpl.php');
	}
	//日志详细
	public function loginfo(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('order','log'));
		$this->odlog = spClass('m_odlog')->spLinker()->find(array('id'=>$id));
		$this->display('order_loginfo.tpl.php');
	}
	//消费统计
	public function usta(){
		$where = ' 1 = 1 ';
		if($search = $this->spArgs('search')){
			if($stime = strtotime($search['stime'])){
				$where .= " AND time>=" . $stime;
				$search['stime'] = $stime;
			}
			if($etime = strtotime($search['etime'])){
				$where .= " AND time<=" . $etime;
				$search['etime'] = $etime;
			}
			$this->search = $search;
		}else{
			if($stime = $this->spArgs('stime')){
				$where .= " AND time>=" . $stime;
				$search['stime'] = $stime;
			}
			if($etime = $this->spArgs('etime')){
				$where .= " AND time<=" . $etime;
				$search['etime'] = $etime;
			}
			$this->search = $search;
		}
		//分页处理
		$page_size = 15;
		$totalnum = spClass('m_odsuc')->findSql("SELECT COUNT(DISTINCT uid) AS totalnum FROM `vn_odsuc` WHERE {$where}");
		$total_count = $totalnum[0]['totalnum'];
		$total_page = ceil($total_count / $page_size);
		$first_page = 1;
		$last_page = $total_page;
		$current_page = max(1, intval($_GET['page']));
		$current_page = min($current_page, $total_page);
		$prev_page = max(1, $current_page - 1);
		$next_page = min($total_page, $current_page + 1);
		$this->pager = array('page_size'=>$page_size,'total_count'=>$total_count,'total_page'=>$total_page,'first_page'=>$first_page,'last_page'=>$last_page,'current_page'=>$current_page,'prev_page'=>$prev_page,'next_page'=>$next_page);
		if($total_count > 0){
			$sql = sprintf("SELECT uid, SUM(money) AS totalmoney, SUM(number) AS prodnumber, COUNT(DISTINCT pid) AS sortnumber FROM `vn_odsuc` WHERE %s GROUP BY uid ORDER BY totalmoney DESC LIMIT %d,%d", $where, ($current_page-1)*$page_size, $page_size);
			$usta = spClass('m_odsuc')->findSql($sql);
			$paiming = 1;
			foreach($usta as &$v){
				$userinfo = spClass('m_user')->spLinker()->find(array('id'=>$v['uid']));
				$v['usercode'] = $userinfo['usercode'];
				$v['username'] = $userinfo['username'];
				$v['ucatname'] = $userinfo['ucat']['ucatname'];
				$v['paiming'] = $paiming + ($current_page-1) * $page_size;
				$paiming++;
			}
			$this->usta = $usta;
		}else{
			$this->usta = null;
		}
		$this->display('order_usta.tpl.php');
	}
	//销量统计
	public function psta(){
		$where = ' 1 = 1 ';
		if($search = $this->spArgs('search')){
			if($stime = strtotime($search['stime'])){
				$where .= " AND time>=" . $stime;
				$search['stime'] = $stime;
			}
			if($etime = strtotime($search['etime'])){
				$where .= " AND time<=" . $etime;
				$search['etime'] = $etime;
			}
			if($bianhao = trim($search['bianhao'])){
				$prodId = spClass('m_prod')->findAll("bianhao LIKE '%{$bianhao}%'", 'id DESC', 'id');
				$ids = '0';
				foreach($prodId as $v){
					$ids .= ',' . $v['id'];
				}
				$where .= " AND pid IN (" . trim($ids, ',') . ")";
				$search['bianhao'] = $bianhao;
			}
			if($pname = trim($search['pname'])){
				$prodId = spClass('m_prod')->findAll("pname LIKE '%{$pname}%'", 'id DESC', 'id');
				$ids = '0';
				foreach($prodId as $v){
					$ids .= ',' . $v['id'];
				}
				$where .= " AND pid IN (" . trim($ids, ',') . ")";
				$search['pname'] = $pname;
			}
			$this->search = $search;
		}else{
			if($stime = $this->spArgs('stime')){
				$where .= " AND time>=" . $stime;
				$search['stime'] = $stime;
			}
			if($etime = $this->spArgs('etime')){
				$where .= " AND time<=" . $etime;
				$search['etime'] = $etime;
			}
			if($bianhao = $this->spArgs('bianhao')){
				$prodId = spClass('m_prod')->findAll("bianhao LIKE '%{$bianhao}%'", 'id DESC', 'id');
				$ids = '0';
				foreach($prodId as $v){
					$ids .= ',' . $v['id'];
				}
				$where .= " AND pid IN (" . trim($ids, ',') . ")";
				$search['bianhao'] = $bianhao;
			}
			if($pname = $this->spArgs('pname')){
				$prodId = spClass('m_prod')->findAll("pname LIKE '%{$pname}%'", 'id DESC', 'id');
				$ids = '0';
				foreach($prodId as $v){
					$ids .= ',' . $v['id'];
				}
				$where .= " AND pid IN (" . trim($ids, ',') . ")";
				$search['pname'] = $pname;
			}
			$this->search = $search;
		}
		//分页处理
		$page_size = 15;
		$totalnum = spClass('m_odsuc')->findSql("SELECT COUNT(DISTINCT pid) AS totalnum FROM `vn_odsuc` WHERE {$where}");
		$total_count = $totalnum[0]['totalnum'];
		$total_page = ceil($total_count / $page_size);
		$first_page = 1;
		$last_page = $total_page;
		$current_page = max(1, intval($_GET['page']));
		$current_page = min($current_page, $total_page);
		$prev_page = max(1, $current_page - 1);
		$next_page = min($total_page, $current_page + 1);
		$this->pager = array('page_size'=>$page_size,'total_count'=>$total_count,'total_page'=>$total_page,'first_page'=>$first_page,'last_page'=>$last_page,'current_page'=>$current_page,'prev_page'=>$prev_page,'next_page'=>$next_page);
		if($total_count > 0){
			$sql = sprintf("SELECT pid, SUM(money) AS totalmoney, SUM(number) AS prodnumber FROM `vn_odsuc` WHERE %s GROUP BY pid ORDER BY prodnumber DESC LIMIT %d,%d", $where, ($current_page-1)*$page_size, $page_size);
			$psta = spClass('m_odsuc')->findSql($sql);
			$paiming = 1;
			foreach($psta as &$v){
				$prodinfo = spClass('m_prod')->find(array('id'=>$v['pid']));
				if(!empty($prodinfo)){
					$v['bianhao'] = $prodinfo['bianhao'];
					$v['pname'] = $prodinfo['pname'];
					if($prodinfo['nosale'] == 1){
						$v['notes'] = '该产品已下架';
					}
				}else{
					$v['notes'] = '该产品已删除';
				}
				$v['paiming'] = $paiming + ($current_page-1) * $page_size;
				$paiming++;
			}
			$this->psta = $psta;
		}else{
			$this->psta = null;
		}
		$this->display('order_psta.tpl.php');
	}
}